package support.com.dzh.myapplication.ui.fragemt

import android.content.DialogInterface
import android.content.Intent
import android.support.v7.app.AlertDialog
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity
import kotlinx.android.synthetic.main.dialog_add_web.view.*
import kotlinx.android.synthetic.main.fragment_collect_web.view.*
import support.com.dzh.myapplication.R
import support.com.dzh.myapplication.base.BaseBean
import support.com.dzh.myapplication.base.BaseFragment
import support.com.dzh.myapplication.model.BeanCollectWeb
import support.com.dzh.myapplication.model.Data
import support.com.dzh.myapplication.presenter.CollectPresenter
import support.com.dzh.myapplication.ui.activity.HomeDetailActivity
import support.com.dzh.myapplication.utils.ToastUtil
import support.com.dzh.myapplication.view.CollectView
import java.util.*


class FragmentCollectWeb : BaseFragment(), BaseQuickAdapter.OnItemChildClickListener {
    class WebAdapter(layout: Int, data: List<BeanCollectWeb>, private val context: FragmentCollectWeb) : BaseQuickAdapter<BeanCollectWeb, BaseViewHolder>(layout, data) {
        override fun convert(helper: BaseViewHolder?, item: BeanCollectWeb?) {

            helper!!.let {
                it.setText(R.id.web_name, item!!.name)
                it.setText(R.id.web_link, item!!.link)
                it.addOnClickListener(R.id.content)
                it.addOnClickListener(R.id.right_menu_2)
                it.addOnClickListener(R.id.right_menu_1)
            }


        }

    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        when (view!!.id) {
            R.id.content -> {
                val intent = Intent(this.activity, HomeDetailActivity::class.java)
                intent.putExtra("url", list.get(position).link)
                startActivity(intent)
            }

            R.id.right_menu_1 -> {
                presenter!!.getDeleteCWeb(list.get(position).id)
                adapter!!.remove(position)
                adapter!!.notifyDataSetChanged()
            }

            R.id.right_menu_2 -> {
                this.position = position
                showAddDialog(1,list.get(position).name!!,list.get(position).link!!,list.get(position).id)
            }

        }
    }

    var list = ArrayList<BeanCollectWeb>()
    var adapter: WebAdapter? = null
    var presenter: CollectPresenter? = null
    var position: Int = -1
    var map: MutableMap<String, String>? = null
    var name = ""
    var link = ""

    override fun layoutId(): Int {
        return R.layout.fragment_collect_web
    }

    override fun initialized() {
        presenter!!.getCollectWeb()

    }


    override fun initListener() {
        mView!!.web_recyclerview.layoutManager = LinearLayoutManager(this.context!!)
        adapter = WebAdapter(R.layout.item_collect_web, list, this)
        mView!!.web_recyclerview.adapter = adapter
        presenter = CollectPresenter(this.context!!, this.activity as RxAppCompatActivity, object : CollectView {
            override fun onUpdateSuccess(data: BaseBean<BeanCollectWeb>) {
                Collections.replaceAll(list as List<BeanCollectWeb>,list.get(position),data.data)
                adapter!!.notifyDataSetChanged()
            }

            override fun onAddWebSuccess(data: BaseBean<BeanCollectWeb>) {
                list.add(data.data!!)
                adapter!!.notifyDataSetChanged()
            }

            override fun onDeleteSuccess() {

            }

            override fun onWebSuccess(data: List<BeanCollectWeb>) {
                list.addAll(data)
                adapter!!.notifyDataSetChanged()
            }

            override fun onSuccess(list: List<Data>) {

            }


            override fun onError(msg: String) {
                ToastUtil.show(context!!, msg)
            }

        })
//        adapter!!.setOnItemClickListener(this)
        adapter!!.setOnItemChildClickListener(this)

    }

    override fun onWClick(view: View) {
    }

    override fun onInvisible() {
    }


    companion object {
        fun newInstance(): FragmentCollectWeb {
            val f = FragmentCollectWeb()
            return f
        }

    }


    fun showAddDialog(type: Int, nameText: String?, linkText: String?, id: Int) {

        val factory = LayoutInflater.from(this.context)
        val view = factory.inflate(R.layout.dialog_add_web, null)

        var builder = AlertDialog.Builder(this.context!!)

        if (type == 1) {
            view.editTextLink.setText(linkText)
            view.editTextName.setText(nameText)
        } else {
            view.editTextLink.setText("")
            view.editTextName.setText("")
        }

        builder.let {
            it.setTitle("收藏网址").setView(view).setNegativeButton("取消", null)
            it.setPositiveButton("确定", object : DialogInterface.OnClickListener {
                override fun onClick(p0: DialogInterface?, p1: Int) {
                    link = view.editTextLink.text.toString()
                    name = view.editTextName.text.toString()

                    if (name.isEmpty() && link.isEmpty()) {
                        ToastUtil.show(this@FragmentCollectWeb.context!!, "请正确填写网站")
                    } else {
                        map = HashMap()
                        map?.put("name", name)
                        map?.put("link", link)
                        if (type == 1) {
                            map?.put("id", id.toString())
                            presenter!!.getUpdateWeb(map!!)
                        } else {
                            presenter!!.getAddWeb(map!!)
                        }
                    }


                }

            })
            it.show()
        }
    }


}
